package com.iolo.sbtest.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * //TODO 添加类/接口功能描述
 *
 * @author Fengxinxin
 * @date 2018-08-28
 */
public class AesEncryptUtil {
    private static String KEY = "1234567890123456";

    private static String IV = "1234567890123456";

    /**
     * 测试
     */
    public static void main(String args[]) throws Exception {

        String test1 = "sa";
        String test = new String(test1.getBytes(), "UTF-8");
        String data = null;
        String key = KEY;
        String iv = IV;
        data = encrypt(test, key, iv);
        System.out.println("数据：" + test);
        System.out.println("加密：" + data);
        String jiemi = desEncrypt(data, key, iv).trim();
        System.out.println("解密：" + jiemi);


        String str = encodeStr(test1);
        System.out.println("64 encode: " + str);
        System.out.println("64 decode: " + decodeStr(str));

    }

    public static String encrypt(String data, String key, String iv) {
        try {
            //算法 模式 补码方式
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            int blockSize = cipher.getBlockSize();

            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }

            byte[] plaintext = new byte[plaintextLength];

            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivsSpec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivsSpec);
            byte[] encrpt = cipher.doFinal(plaintext);

            return new Base64().encodeToString(encrpt);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String desEncrypt(String data, String key, String iv) throws Exception {
        try {
            byte[] encrypted1 = new Base64().decode(data);

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decodeStr(String pwd) {
        Base64 base64 = new Base64();
        byte[] debytes = base64.decode(new String(pwd).getBytes());
        return new String(debytes);
    }

    public static String encodeStr(String pwd) {
        Base64 base64 = new Base64();
        byte[] debytes = base64.encode(new String(pwd).getBytes());
        return new String(debytes);
    }
}
